<?php
/**
 * 前台今日食餐模块
 * 展示本周食谱
 */
#定义命名空间
namespace Home\Controller;
class FoodController extends ComController{
    
    /**
     * 今日食餐列表
     * 本周
     */
    public function index(){
        $food = M('food');//实例化食谱主表
        $food_time = M('food_time');//实例化食谱辅表
        $sc_id = I('get.uid') ? I('get.uid') : 0;
        $time = $this->getTime();//获取本周周一与周五的时间戳
        $prefix = C('DB_PREFIX');//读取表前缀
        $where = "{$prefix}food.sc_id = ".$sc_id;//查询条件
        $where .= " and {$prefix}food.is_del = 1";
        $where .= " and {$prefix}food_time.time >= ".$time['mon'];
        $where .= " and {$prefix}food_time.time <= ".$time['fri'];
        $order = 'time';//排序
        $data = $food->field("{$prefix}food_time.*,{$prefix}member.name")
                     ->join("inner join {$prefix}food_time on {$prefix}food.id = {$prefix}food_time.food_id")
                     ->join("inner join {$prefix}member on {$prefix}food.doctor = {$prefix}member.uid")
                     ->where($where)->order($order)->select();
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 获取本周周一与周五的时间戳
     */
    public function getTime(){
        $nowTime = time(); //时间戳
        $nowTime = date('Y-m-d',$nowTime);
        $nowTime = strtotime($nowTime);
        // $nowTime = strtotime('2017/7/1');
        $nowDay = date('N',$nowTime);//周几
        $day = 3600*24;//一天时间
        $data = array();
        switch ($nowDay) {
            case '1':
                   $data['mon'] = $nowTime;
                   $data['fri'] = $nowTime+($day*5)-1;
                break;
            case '2':
                   $data['mon'] = $nowTime-($day*1);
                   $data['fri'] = $nowTime+($day*4)-1;
                break;
            case '3':
                   $data['mon'] = $nowTime-($day*2);
                   $data['fri'] = $nowTime+($day*3)-1;
                break;
            case '4':
                   $data['mon'] = $nowTime-($day*3);
                   $data['fri'] = $nowTime+($day*2)-1;
                break;
            case '5':
                   $data['mon'] = $nowTime-($day*4);
                   $data['fri'] = $nowTime+($day*1)-1;
                break;
            case '6':
                   $data['mon'] = $nowTime-($day*5);
                   $data['fri'] = $nowTime-($day*0)-1;
                break;   
            default:
                   $data['mon'] = $nowTime-($day*6);
                   $data['fri'] = $nowTime-($day*1)-1;
                break;
        }
        return $data;
    }
}